/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    2013 year
 *    Author: Esipenko Aleksey (Alexorleon)
 *    E-Mail: alexorleon@gmail.com
 */
#include "pageData.h"
//#include <QPainter>
//#include <QPrinter>
#include <QTextDocument>
#include <QTextDocumentFragment>
#include <QDate>
#include <QDebug>

PageData::PageData()
    : QObject(){
}

void PageData::setParameters(const QList<int> &list){

    parameters = list;
}

void PageData::createPage(const QStringList &list_1, const QStringList &list_2,
                          const QStringList &list_3){

    qDebug()<<"BEGIN";

    qDebug()<<"1";
    OnePage page;
    page.dateCreate = list_1.at(0);
    page.qrText = list_1.at(1);
    page.performer = list_1.at(2);

    qDebug()<<"2";
    QStringList tempListPlaces;
    for(int j = 0; j < list_2.count(); j+=4){

        tempListPlaces.append(list_2.at(j));
        tempListPlaces.append(list_2.at(j+1));
        tempListPlaces.append(list_2.at(j+2));
        tempListPlaces.append(list_2.at(j+3));
        page.listPlace << tempListPlaces;
        tempListPlaces.clear();
    }

    qDebug()<<"3";
    QStringList tempListHistory;
    for(int l = 0; l < list_3.count(); l+=2){

        tempListHistory.append(list_3.at(0));
        tempListHistory.append(list_3.at(1));
        page.listHistory << tempListHistory;
        tempListHistory.clear();
    }

    pages << page;
}

void PageData::populateDocument(QTextDocument *document){

    QString html("<html>\n<body>\n");
    for (int page = 0; page < pages.count(); ++page) {
        html += pageAsHtml(page, false);
        if (page + 1 < pages.count())
            html += "<br style='page-break-after:auto; text-align: left;'/>\n";
    }
    html += "</body>\n</html>\n";
    document->setHtml(html);
}

QString PageData::pageAsHtml(int page, bool selfContained){

    const OnePage &thePage = pages.at(page);

    QString html;
    if (selfContained)
        html += "<html>\n<body>\n";
    html += QString("<p style='font-size:%1pt'>%2   %3</p>\n").arg(Qt::escape(QString::number(parameters.at(4))))
            .arg(Qt::escape(thePage.dateCreate)).arg(Qt::escape(thePage.performer));
    html += "<p>";
    html += QString("<p style='font-size:%1pt'>%2</p>").arg(Qt::escape(QString::number(parameters.at(5))))
            .arg(Qt::escape(thePage.qrText));
    html += "</p>\n";
    html += "<p>";
    // если нет таблицы, то и рисовать незачем
    if(thePage.listPlace.isEmpty()){

        html += QString("<p style='font-size:%1pt'>%2</p>").arg(Qt::escape(QString::number(parameters.at(6))))
                .arg(Qt::escape(tr("Не размещен")));
        html += "</p>\n";
        html += "<p>";
    }else{

        html += QString("<p style='font-size:%1pt'>%2</p>").arg(Qt::escape(QString::number(parameters.at(6))))
                .arg(Qt::escape(tr("Места размещений:")));
        html += "</p>\n";
        html += "<p>";
        html += itemsAsHtmlTable(thePage);
        html += "</p>\n";
        html += "<p>";
    }

    // если нет историй, меняем заголовок
    if(thePage.listHistory.size() > 0){

        html += QString("<p style='font-size:%1pt'>%2</p>").arg(Qt::escape(QString::number(parameters.at(6))))
                .arg(Qt::escape(tr("История:")));
        html += "</p>\n";
        // Записываем историю
        for(int l = 0; l < thePage.listHistory.size(); ++l){

            html += QString("<p style='font-size:%1pt'>%2   %3</p>").arg(Qt::escape(QString::number(parameters.at(8))))
                    .arg(Qt::escape(thePage.listHistory.at(l).at(0))).arg(thePage.listHistory.at(l).at(1));
        }
    }else{

        html += QString("<p style='font-size:%1pt'>%2</p>").arg(Qt::escape(QString::number(parameters.at(6))))
                .arg(Qt::escape(tr("Историй нет")));
        html += "</p>\n";
    }
    if (selfContained)
        html += "</body>\n</html>\n";
    return html;
}

QString PageData::itemsAsHtmlTable(const OnePage &thePage){

    QString html("<table border='1' cellpadding='5' width='100%'>");

    html += "<tr>\n";
    html += QString("<td align='center'> <p style='font-size:%1pt'>%2</p></td>\n")
            .arg(Qt::escape(QString::number(parameters.at(7)))).arg(Qt::escape(tr("Дата")));
    html += QString("<td align='center'> <p style='font-size:%1pt'>%2</p></td>\n")
            .arg(Qt::escape(QString::number(parameters.at(7)))).arg(Qt::escape(tr("Место размещения")));
    html += QString("<td align='center'> <p style='font-size:%1pt'>%2</p></td>\n")
            .arg(Qt::escape(QString::number(parameters.at(7)))).arg(Qt::escape(tr("Телефон")));
    html += QString("<td align='center'> <p style='font-size:%1pt'>%2</p></td>\n")
            .arg(Qt::escape(QString::number(parameters.at(7)))).arg(Qt::escape(tr("e-mail")));

    for(int l = 0; l < thePage.listPlace.size(); ++l){

        html += "<tr>\n";
        for(int i = 0; i < thePage.listPlace.at(l).count(); ++i){
            html += QString("<td align='center'>"
                            "<p style='font-size:%1pt'>%2</p></td>\n")
                            .arg(Qt::escape(QString::number(parameters.at(7)))).arg(Qt::escape(thePage.listPlace.at(l).at(i)));
        }
        html += "</tr>\n";
    }
    //if (!html.endsWith("</tr>\n"))
      //  html += "</tr>\n";
    html += "</table>\n";
    return html;
}
